package vn.co.thn.android.videohot.activity;

import java.util.ArrayList;

import vn.amobi.util.ads.AmobiAdView;
import vn.amobi.util.ads.AmobiAdView.WidgetSize;
import vn.co.thn.android.serviceconnect.callback.RequestCallBack;
import vn.co.thn.android.serviceconnect.request.Param;
import vn.co.thn.android.serviceconnect.request.RequestService;
import vn.co.thn.android.videohot.R;
import vn.co.thn.android.videohot.VHApp;
import vn.co.thn.android.videohot.VHConstants;
import vn.co.thn.android.videohot.VHUrlApi;
import vn.co.thn.android.videohot.common.VHCommonYouTubeActivity;
import vn.co.thn.android.videohot.data.VHVideoItemData;
import android.os.Bundle;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayerFragment;
import com.startapp.android.publish.StartAppAd;
import com.startapp.android.publish.StartAppSDK;
import com.startapp.android.publish.splash.SplashConfig;
import com.startapp.android.publish.splash.SplashConfig.Theme;

public class VHPluginPlayActivity extends VHCommonYouTubeActivity {
	YouTubePlayerFragment youTubePlayerFragment;
	private VHVideoItemData video;
	private TextView title, content;
	AmobiAdView adView;

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		overridePendingTransition(R.anim.fragment_slide_right_enter,
				R.anim.fragment_slide_left_exit);
		setContentView(R.layout.vh_plugin_play_activity);
		youTubePlayerFragment = (YouTubePlayerFragment) getFragmentManager()
				.findFragmentById(R.id.youtube_fragment);
		title = (TextView) findViewById(R.id.title);
		content = (TextView) findViewById(R.id.content);
		String key = getIntent().getData().getQueryParameter("key");
		LoadData(key);
		setView(key);
		adView = (AmobiAdView) findViewById(R.id.main_menu_adView);
		if (adView != null) {
			// adview.setEventListener(this);
			adView.loadAd(WidgetSize.SMALL);
			// adView.scheduleRefresh(30000);
			adView.setHideAfterClick(false);
			// adview.stopRefresh();
		}
		StartAppSDK.init(this, getString(R.string.start_dev),
				getString(R.string.start_id), true);
		StartAppAd.showSplash(this, arg0, new SplashConfig()
				.setTheme(Theme.SKY).setLogo(R.drawable.ic_launcher)
				.setAppName(getString(R.string.sms_load_video)));
	}

	private void LoadData(String key) {
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("key", key));
		RequestService requestService = new RequestService(
				VHUrlApi.Api_Video_Item, VHVideoItemData.class,
				new RequestCallBack<VHVideoItemData>() {

					@Override
					public void onResult(VHVideoItemData data, boolean isUpdate) {
						// TODO Auto-generated method stub
						video = data;
						title.setText(video.getTitle());
						content.setText(video.getContent().getValue());
						youTubePlayerFragment.initialize(VHConstants.API_KEY,
								VHPluginPlayActivity.this);
					}

					@Override
					public void onResult(ArrayList<VHVideoItemData> data,
							boolean isUpdate) {
						// TODO Auto-generated method stub

					}


				}, VHApp.getInstance());
		requestService.setParams(params);
		requestService.setRequestError(this);
		requestService.Post();
	}

	private void setView(String key) {
		ArrayList<Param> params = new ArrayList<Param>();
		params.add(new Param("key", key));
		RequestService requestService = new RequestService(
				VHUrlApi.Api_Video_set_review, VHVideoItemData.class,
				new RequestCallBack<VHVideoItemData>() {

					@Override
					public void onResult(VHVideoItemData data, boolean isUpdate) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onResult(ArrayList<VHVideoItemData> data,
							boolean isUpdate) {
						// TODO Auto-generated method stub

					}


				}, VHApp.getInstance());
		requestService.setParams(params);
		requestService.setRequestError(this);
		requestService.Post();
	}

	@Override
	public void onInitializationSuccess(YouTubePlayer.Provider provider,
			YouTubePlayer player, boolean wasRestored) {
		if (!wasRestored) {
			player.cueVideo(video.getEmbed_code());
		}
	}

	@Override
	protected YouTubePlayer.Provider getYouTubePlayerProvider() {
		return (YouTubePlayerFragment) getFragmentManager().findFragmentById(
				R.id.youtube_fragment);
	}

	@Override
	public void onError(VolleyError error) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onException(Exception e) {
		// TODO Auto-generated method stub
		
	}

}
